package com.ruifu.doc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fhzncloud.cloud.common.security.service.CloudUser;
import com.fhzncloud.cloud.common.security.util.SecurityUtils;
import com.ruifu.common.po.ObjectPermissionsAuthorization;
import com.ruifu.doc.mapper.ObjectPermissionsAuthorizationMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author lcq
 * @create 2020/4/23
 */
@RestController
@Api(tags = "权限控制")
@RequestMapping("/permissions")
public class PermissionsController {
    @Autowired(required = false)
    private ObjectPermissionsAuthorizationMapper objectPermissionsAuthorizationMapper;
    /**
     * 用于存储登陆人信息
     */
    private CloudUser cloudUser;

    /**
     * 通过url请求方法之前,调用该方法获取登陆人信息
     */
    @ModelAttribute
    public void before() {
        cloudUser = SecurityUtils.getUser();
    }

    @PostMapping("/insert")
    @ApiOperation(value = "权限新增", notes = "传入参数为权限对象实体")
    public R inset(@RequestBody ObjectPermissionsAuthorization objectPermissionsAuthorization){
        try {
            objectPermissionsAuthorizationMapper.insert(objectPermissionsAuthorization);
            return R.ok("新建成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("新建失败");
        }

    }

    @GetMapping("/delete/{permissionsAuthorizationId}")
    @ApiOperation(value = "权限删除", notes = "传入参数为权限主键id")
    public R delete(@PathVariable Long permissionsAuthorizationId){
        try {
            objectPermissionsAuthorizationMapper.deleteById(permissionsAuthorizationId);
            return R.ok("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("删除失败");
        }
    }

    @PostMapping("/update")
    @ApiOperation(value = "权限修改", notes = "传入参数为权限对象实体")
    public R update(@RequestBody ObjectPermissionsAuthorization objectPermissionsAuthorization){
        try {
            objectPermissionsAuthorizationMapper.updateById(objectPermissionsAuthorization);
            return R.ok("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.failed("修改失败");
        }
    }

    @GetMapping("/findById/{permissionsAuthorizationId}")
    @ApiOperation(value = "权限详情", notes = "传入参数为权限主键id")
    public R findById(@PathVariable Long permissionsAuthorizationId){
        return R.ok(objectPermissionsAuthorizationMapper.selectById(permissionsAuthorizationId));
    }

    @GetMapping("/findByObjId/{authorizationObjId}")
    @ApiOperation(value = "查询选中对象的规则列表", notes = "传入参数为授权对象id")
    public R findByObjId(@PathVariable String authorizationObjId){
        return R.ok(
                objectPermissionsAuthorizationMapper.selectList(
                     Wrappers.<ObjectPermissionsAuthorization>lambdaQuery()
                    .eq(ObjectPermissionsAuthorization::getAuthorizationObject,authorizationObjId)
                    .eq(ObjectPermissionsAuthorization::getCreator,cloudUser.getId())
                    .eq(ObjectPermissionsAuthorization::getSetSid,cloudUser.getTenantId())
                )
        );
    }
    @PostMapping("/page/{current}/{size}")
    @ApiOperation(value = "权限列表", notes = "传入参数为current = 0L;size = 10L;")
    public R<IPage<ObjectPermissionsAuthorization>> findPermissionsPage(@PathVariable Long current,
                                                                        @PathVariable Long size,
                                                                        @RequestBody ObjectPermissionsAuthorization objectPermissionsAuthorization){
        return R.ok(objectPermissionsAuthorizationMapper.selectPage(new Page<>(current,size),
                Wrappers.query(new ObjectPermissionsAuthorization()).lambda()
                        .eq(ObjectPermissionsAuthorization::getSetSid,cloudUser.getTenantId())
                        .eq(objectPermissionsAuthorization.getObjectTypeId()!=null,
                                ObjectPermissionsAuthorization::getObjectTypeId,
                                objectPermissionsAuthorization.getObjectTypeId())
                        .eq(objectPermissionsAuthorization.getCreator()!=null,
                                ObjectPermissionsAuthorization::getCreator,
                                cloudUser.getId())));
    }
}
